package main

import (
	"encoding/binary"
	"flag"
	"fmt"
	"io"
	"os"
	"time"
)

var agr1 = flag.Int("memint", 1, "设置单次拷贝大小.")

func main() {
	flag.Parse()
	var buf = make([]byte, 1024*1024)
	var num int
	//1.二进制写文件
	fd3, err := os.Create(os.Args[2])
	if err != nil {
		fmt.Println(err)
		return
	}
	//2.读文件
	fdr, err := os.Open(os.Args[1])
	if err != nil {
		fmt.Println(err)
		return
	}
	finfo, _ := fdr.Stat()
	fsize := finfo.Size()
	totaltime1 := time.Now()
	for {
		t1 := time.Now()
		n, err := fdr.Read(buf)
		if err == io.EOF || n == 0 {
			break
		}
		err = binary.Write(fd3, binary.LittleEndian, buf[:n])
		if err != nil {
			fmt.Println(err)
			break
		}
		num += n
		rate := num * 100 / int(fsize)
		t2 := time.Now()
		subtime := t2.Sub(t1).Seconds()
		fmt.Print("\r进度：[", walkstring(rate, 100), "]速度：", num/(1024*1024), "MiB ", int(float64(n)/(1024*1024*subtime)), "MiB/s百分比：", rate, "%")
	}
	totaltime2 := time.Now()
	defer fmt.Print(" total_time ", totaltime2.Sub(totaltime1).Seconds(), "second\n")
}

//进度条
func walkstring(nu int, total int) string {
	var istr, jstr string
	for i := 0; i < nu; i++ {
		istr += ">"
	}
	for j := 0; j < ((total) - nu - 1); j++ {
		jstr += "-"
	}
	return istr + jstr
}
